package com.bdmc.web.controller.device;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.base.domain.BaseDevice;
import com.bdmc.base.service.IBaseDeviceService;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.device.domain.DevHealthRealtime;
import com.bdmc.device.domain.vo.DevGlucoseDataVo;
import com.bdmc.device.domain.vo.DevSleepDataVo;
import com.bdmc.device.domain.vo.DevSportDataVo;
import com.bdmc.device.service.*;
import com.bdmc.web.controller.api.http.WatchSetting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/device/watch")
public class DeviceWatchController extends BaseController {
    @Autowired
    IDevGlucoseDataService glucoseDataService;
    @Autowired
    IDevSleepDataService sleepDataService;
    @Autowired
    IDevSportDataService sportDataService;
    @Autowired
    IDevHealthDataService healthDataService;
    @Autowired
    IDevHealthRealtimeService healthRealtimeService;
    @Autowired
    IDevGpsRealtimeService realtimeService;

    @Autowired
    IBaseDeviceService deviceService;

    @GetMapping("glucose")
    public AjaxResult<DevGlucoseDataVo> getGlucoseData(String imei){

        return AjaxResult.success(glucoseDataService.getVoByImei(imei));
    }

    @GetMapping("sleep")
    public AjaxResult<DevSleepDataVo> getSleepDataService(String imei){
        return AjaxResult.success(sleepDataService.getVoByImei(imei));
    }

    @GetMapping("health")
    public AjaxResult<DevHealthRealtime> getHealthData(String imei){
        return AjaxResult.success(healthRealtimeService.getByImei(imei));
    }

    @GetMapping("sport")
    public AjaxResult<DevSportDataVo> getSportData(String imei){
        return AjaxResult.success(sportDataService.getVoByImei(imei));
    }


    /**
     * 设置手表参数
     * @param request 请求参数，包含 deviceId 和 其他参数
     * @return AjaxResult
     */
    @PostMapping("/setting")
    public AjaxResult<Void> setStep(@RequestBody JSONObject request) {
        String deviceIds = request.getStr("deviceIds");
        JSONObject params = request.getJSONObject("params");
        String result = WatchSetting.setBatchParams(deviceIds,params);
        String failDeviceStr = result.substring(result.indexOf("失败："));
        List<String> deviceList = new ArrayList<>();
        String[] deviceIdArr = deviceIds.split(",");
        for (String devId : deviceIdArr) {
            if(!failDeviceStr.contains(devId)){
                deviceList.add(devId);
            }
        }
        if(deviceList.size()>0){
            LambdaUpdateWrapper<BaseDevice> w = Wrappers.lambdaUpdate();
            w.set(BaseDevice::getSettingParams,1).in(BaseDevice::getImei,deviceList);
            deviceService.update(w);
        }
        return AjaxResult.success(result);
    }



//    /**
//     * 设置SOS联系人
//     * @param request 请求参数，包含 deviceId、sos1、sos2 和 sos3
//     * @return AjaxResult
//     */
//    @PostMapping("/setSOSContact")
//    public AjaxResult<Void> setSOSContact(@RequestBody JSONObject request) {
//        String deviceId = request.getStr("deviceId");
//        String sos1 = request.getStr("sos1");
//        String sos2 = request.getStr("sos2");
//        String sos3 = request.getStr("sos3");
//        Boolean result = WatchSetting.setSOSContact(deviceId, sos1, sos2, sos3);
//        return toAjax(result);
//    }
//
//    /**
//     * 清除SOS联系人
//     * @param request 请求参数，包含 deviceId
//     * @return AjaxResult
//     */
//    @PostMapping("/clearSOSContact")
//    public AjaxResult<Void> clearSOSContact(@RequestBody JSONObject request) {
//        String deviceId = request.getStr("deviceId");
//        Boolean result = WatchSetting.clearSOSContact(deviceId);
//        return toAjax(result);
//    }

    /**
     * 设置定位上传间隔(分钟)
     * @param request 请求参数，包含 deviceId 和 interval
     * @return AjaxResult
     */
    @PostMapping("/setLocationTimeInterval")
    public AjaxResult<Void> setLocationTimeInterval(@RequestBody JSONObject request) {
        String deviceId = request.getStr("deviceId");
        int interval = request.getInt("interval");
        Boolean result = WatchSetting.setLocationTimeInterval(deviceId, interval);
        return toAjax(result);
    }

    /**
     * 设置心率上传间隔和安全阀值
     * @param request 请求参数，包含 deviceId、maxHeartRate、minHeartRate 和 interval
     * @return AjaxResult
     */
    @PostMapping("/setHeartRateParams")
    public AjaxResult<Void> setHeartRateParams(@RequestBody JSONObject request) {
        String deviceId = request.getStr("deviceId");
        int maxHeartRate = request.getInt("maxHeartRate");
        int minHeartRate = request.getInt("minHeartRate");
        int interval = request.getInt("interval");
        Boolean result = WatchSetting.setHeartRateParams(deviceId, maxHeartRate, minHeartRate, interval);
        return toAjax(result);
    }

    /**
     * 设置血压上传安全阀值
     * @param request 请求参数，包含 deviceId、bpLowMin、bpLowMax、bpHighMin、bpHighMax 和 bpi
     * @return AjaxResult
     */
    @PostMapping("/setBloodPressureParams")
    public AjaxResult<Void> setBloodPressureParams(@RequestBody JSONObject request) {
        String deviceId = request.getStr("deviceId");
        int bpLowMin = request.getInt("bpLowMin");
        int bpLowMax = request.getInt("bpLowMax");
        int bpHighMin = request.getInt("bpHighMin");
        int bpHighMax = request.getInt("bpHighMax");
        int bpi = request.getInt("bpi");
        Boolean result = WatchSetting.setBloodPressureParams(deviceId, bpLowMin, bpLowMax, bpHighMin, bpHighMax, bpi);
        return toAjax(result);
    }

    /**
     * 设置血氧上传安全阀值
     * @param request 请求参数，包含 deviceId、bloodOxMax 和 bloodOxMin
     * @return AjaxResult
     */
    @PostMapping("/setBloodOxygenParams")
    public AjaxResult<Void> setBloodOxygenParams(@RequestBody JSONObject request) {
        String deviceId = request.getStr("deviceId");
        int bloodOxMax = request.getInt("bloodOxMax");
        int bloodOxMin = request.getInt("bloodOxMin");
        Boolean result = WatchSetting.setBloodOxygenParams(deviceId, bloodOxMax, bloodOxMin);
        return toAjax(result);
    }
}
